// pages/park/park.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.getLocation({
      // 使用gjc02方便使用openLocation打开第三方地图
      type: "gcj02",
      isHighAccuracy: true,
      success: res => {
        console.log("定位成功：", res);
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        // 标记当前位置
        let markers: any = this.data.markers;
        markers[0] = {
          id: 0,
          title: "我的位置",
          longitude: res.longitude,
          latitude: res.latitude,
          width: 15,
          height: 15,
        }
        
        // 异步请求附近停车场数据
        setTimeout(() => {
          let listData:any = [
            {
              pid: 1,
              parkAddress: "天府五街100号",
              parkPrice: 0,
              longitude: 104.072948,
              latitude: 30.550234,
              spaceDiscount: 0,
              distance: 0,
              parkName: "超级停车场",
              maxSpace: 100,
              nowSpace: 10
            },
            {
              pid: 2,
              parkAddress: "天府五街101号",
              parkPrice: 0,
              longitude: 104.072373,
              latitude: 30.550203,
              spaceDiscount: 0,
              distance: 0,
              parkName: "超级停车场2",
              maxSpace: 180,
              nowSpace: 20
            },
            {
              pid: 3,
              parkAddress: "天府五街101号",
              parkPrice: 0,
              longitude: 104.068062,
              latitude: 30.550965,
              spaceDiscount: 0,
              distance: 0,
              parkName: "超级停车场3",
              maxSpace: 180,
              nowSpace: 20
            }
          ];
          // 画出停车场图标
          listData.forEach((e:any) => {
            let m = markers.find((e2:any) => e2.pid == e.pid);
            if (!m) {
              markers.push({
                id: e.pid,
                longitude: e.longitude,
                latitude: e.latitude,
                title: e.parkName,
                width: 15,
                height: 15,
                iconPath: "../../asset/tcc.png"
              })
            }
          });
          console.log("markers = ",markers);

          this.setData({listData,markers});
        },500)
      },
      fail: res => {
        console.log("定位失败：", res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 点击图标
  handleMarkerTap(e: any) {
    console.log("图标点击：",e);
    this.changeMarkerIcon(e.markerId);
  },
  // 点击子组件列表项，触发的事件
  handleItemTap(e:any) {
    // console.log("子组件点击：",e);
    this.changeMarkerIcon(e.detail);
  },
  changeMarkerIcon(selectId:string) {
    let markers = this.data.markers;
    markers.forEach((v:any) => {
      if (v.id > 0) {
        if (v.id == selectId) {
          v.iconPath = "../../asset/tcc-s.png";
          // 可以添加代码，让地图居中
          // 
        } else {
          v.iconPath = "../../asset/tcc.png";
        }
      }
    })
    this.setData({markers,selectId});
  }

})